/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * OcroPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * OcroPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with OcroPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.text;

import eu.himeros.util.IoManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class CharWriter {
    BufferedReader allBr=null; // all characters
    BufferedReader someBr=null; // characters actually observed in a given text (e.g. Athenaeus)
    BufferedWriter bw=null;
    String[] bounds=null;
    String[] charOccs=null; // chars + occurrences
    int uppBound=0;
    int lowBound=0;
    Vector<String> allCharsV=new Vector<String>(1000);
    Vector<String> someCharsV=new Vector<String>(1000);
    Vector<Integer> timesCharsV=new Vector<Integer>(1000);

    /**
     *
     */
    public CharWriter(){}

    /**
     *
     * @param args
     */
    public CharWriter(String[] args){
        init(args);
    }

    /**
     *
     * @param args
     */
    public void init(String[] args){
        String line=null;
        try{
            allBr=IoManager.getReader(args[0]);
            someBr=IoManager.getReader(args[1]);
            bw=IoManager.getWriter(args[2]);
            while((line=someBr.readLine())!=null){
                charOccs=line.split("\t");
                someCharsV.add(charOccs[0]);
                timesCharsV.add(Integer.valueOf(charOccs[1]));                
            }            
            while((line=allBr.readLine())!=null){
                bounds=line.split("\\-");
                lowBound=Integer.parseInt(bounds[0],16);
                uppBound=Integer.parseInt(bounds[1],16);
                for(int i=lowBound;i<uppBound+1;i++){
                    allCharsV.add(""+(char)i);                    
                }
            }
            for(String charX:allCharsV){
                int idx=someCharsV.indexOf(charX);
                if(idx==-1){
                    bw.write(charX+" "+4);bw.newLine();
                }else{
                    bw.write(charX+" "+timesCharsV.elementAt(idx));bw.newLine();
                }
            }
            bw.close(); 
        }catch(IOException ex){ex.printStackTrace();}
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args){
        if(args==null||args.length==0){
            System.out.println("It generates ad hoc unicharset for tesseract");
            System.out.println("Usage: java eu.himeros.ocrtrainer.CharWriter <unicodeRangesFile> <charsAndOccurrencesFile> <resultFile>");
            System.out.println("<unicodeRAngesFile> contains lines of ranges in the form: \\uxxxx-\\uyyyy");
            System.out.println("<charsAndOccurrencesFile> contains occurrences of each char in the format: char<tab>occurrences");
            System.out.println("<resultFile> contains results");
            System.exit(1);
        }
        CharWriter cw=new CharWriter(args);
    }
        
}

